package routes

import (
	"api/controller"
	"api/core/env"
	"api/core/errmsg"
	"api/midlib"
	"api/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func InitRouter() {
	gin.SetMode(env.App.AppMode)
	r := gin.New()
	r.Use(midlib.Log())
	r.Use(midlib.CORS())

	r.StaticFS("/static", http.Dir("../web/static"))
	r.StaticFile("/favicon.ico", "/web/favicon.ico")

	r.GET("/hi", func(c *gin.Context) {
		u := model.User{
			Username: "admin",
			Password: "admin123",
		}
		code := model.CreateUser(&u)
		c.JSON(http.StatusOK, gin.H{
			"msg": errmsg.GetErrMsg(code),
		})
	})

	auth := r.Group("api")
	auth.Use(midlib.JwtToken())
	{
		// 上传文件
		auth.POST("upload", controller.Upload)
		// 更新个人设置
		auth.GET("admin/profile/:id", controller.GetProfile)
		auth.PUT("profile/:id", controller.UpdateProfile)
		//修改密码
		auth.PUT("admin/changepw/:id", controller.ChangeUserPassword)

		//用户
		auth.GET("admin/users", controller.GetUsers)
		auth.PUT("user/:id", controller.EditUser)
		auth.DELETE("user/:id", controller.DeleteUser)

		// 分类
		auth.GET("admin/category", controller.GetCate)
		auth.POST("category/add", controller.AddCategory)
		auth.PUT("category/:id", controller.EditCate)
		auth.DELETE("category/:id", controller.DeleteCate)

		// 文章
		auth.GET("admin/article/info/:id", controller.GetArtInfo)
		auth.GET("admin/article", controller.GetArt)
		auth.POST("article/add", controller.AddArticle)
		auth.PUT("article/:id", controller.EditArt)
		auth.DELETE("article/:id", controller.DeleteArt)

		// 评论
		auth.GET("comment/list", controller.GetCommentList)
		auth.DELETE("delcomment/:id", controller.DeleteComment)
		auth.PUT("checkcomment/:id", controller.CheckComment)
		auth.PUT("uncheckcomment/:id", controller.UnCheckcomment)

	}

	router := r.Group("api")
	{
		//登录
		router.POST("admin/login",controller.LoginBackend)
		router.POST("user/login",controller.LoginFrontend)
		//个人信息
		router.GET("profile/:id", controller.GetProfile)

		//用户信息
		router.GET("user/:id",controller.GetUserInfo)
		router.POST("user/add", controller.AddUser)
		//router.GET("users", controller.GetUsers)

		// 分类
		router.GET("category", controller.GetCate)
		router.GET("category/:id", controller.GetCateInfo)

		// 文章
		router.GET("article", controller.GetArt)
		router.GET("article/list/:id", controller.GetCateArt)
		router.GET("article/info/:id", controller.GetArtInfo)

		//评论
		router.POST("addcomment", controller.AddComment)
		router.GET("comment/info/:id", controller.GetComment)
		router.GET("commentfront/:id", controller.GetCommentListFront)
		router.GET("commentcount/:id", controller.GetCommentCount)
	}

	r.Run(env.App.HttpPort)
}